﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Management;

namespace PerformanceCounterTestApp
{
    public partial class Form1 : Form
    {
        string procName = Process.GetCurrentProcess().ProcessName;            
        
        public Form1()
        {
            InitializeComponent();
        }
              
        
        private void buttonGet_Click(object sender, EventArgs e)
        {
            string[] cats =
            {
                "System",
                ".NET CLR Memory",
                "Process",
                "Processor",
                "Memory",                
            };        
            this.Cursor = Cursors.WaitCursor;
            StringBuilder sb = new StringBuilder();                        
            foreach (string catName in cats)
                sb.AppendLine(new PerformanceInfo(catName, procName).Aquire().ToString());
            this.textBox1.Text = sb.ToString();            
            this.Cursor = Cursors.Default;
        }
        private void buttonMemInfo_Click(object sender, EventArgs e)
        {
            string[] countersProcess = 
            {
                "Private Bytes",
                "Virtual Bytes",
                "Working Set",
                "Thread Count"
            };
            string[] countersNet = 
            {
                "# Bytes in all Heaps"
            };
            this.Cursor = Cursors.WaitCursor;
            string info = new PerformanceInfo("Process", procName, countersProcess).Aquire().ToString();
            info += new PerformanceInfo(".NET CLR Memory", procName, countersNet).Aquire().ToString();
            this.textBox1.Text = info;
            this.Cursor = Cursors.Default;
        }

        private void buttonSet_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            new CreatePermanceInfoObject().GenerateNextValues();
            textBox1.Text = new PerformanceInfo(CreatePermanceInfoObject.CategoryName, procName).Aquire().ToString();
            this.Cursor = Cursors.Default;
        }

       
        private void buttonSystemInfo_Click(object sender, EventArgs e)
        {
            string[] wmi = {
                //"Win32_DiskDrive",
                "Win32_OperatingSystem",
                "Win32_Processor",
                "Win32_ComputerSystem",
                //"Win32_StartupCommand",
                //"Win32_ProgramGroup",
                //"Win32_SystemDevices"
            };     
            this.Cursor = Cursors.WaitCursor;            
            StringBuilder sb = new StringBuilder();
            foreach(string i in wmi)
            {                
                QueryWMI(i, sb);             
            }
             textBox1.Text = sb.ToString();                    
            this.Cursor = Cursors.Default;    
        }
        StringBuilder Append(string query, StringBuilder dest)
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
            foreach (ManagementObject mo in searcher.Get())
            {
                foreach (PropertyData pd in mo.Properties)
                {
                    if (pd.Value == null)
                        continue;
                    string value = pd.Value.ToString();
                    if (value.Length > 0)
                        dest.AppendLine(string.Format("  {0} - {1}", pd.Name, value));
                }
            }
            return dest;
        }        

        public void QueryWMI(string queryObject, StringBuilder sb)
        {            
            try
            {
                sb.AppendLine(queryObject);
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM " + queryObject);
                foreach (ManagementObject mo in searcher.Get())
                {                    
                    foreach (PropertyData pd in mo.Properties)
                    {
                        if (pd.Value == null)
                            continue;
                        string value = pd.Value.ToString();
                        if (value.Length > 0)
                            sb.AppendLine(string.Format("  {0} - {1}", pd.Name, value));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            sb.AppendLine("");
        }

        private void buttonWMI2_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            StringBuilder sb = new StringBuilder();
            string query =
               "Select Caption, Version, ServicePackMajorVersion, ServicePackMinorVersion from Win32_OperatingSystem";            
            textBox1.Text = Append(query, sb).ToString(); ;
            this.Cursor = Cursors.Default;    
        }       
    }
}
